package com.jy.modules.cims.cooperorgForm.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.controller.CimsBaseController;
import com.jy.modules.cims.common.exception.CimsExceptionConstant;
import com.jy.modules.cims.common.exception.SysAssistException;
import com.jy.modules.cims.component.sysAssist.ExportAndImport;
import com.jy.modules.cims.data.dto.CooperorgFormCreditTotalDto;
import com.jy.modules.cims.deal.service.LoanDealService;
import com.jy.modules.cims.subsidiary.service.SubsidiaryService;
import com.jy.platform.core.message.DataMsg;
import com.jy.platform.core.message.PageParameter;
import com.jy.platform.core.message.PageUtil;
import com.jy.platform.core.message.QueryReqBean;
import com.jy.platform.restservice.exception.AbaboonException;

/**
 * 统计报表
 * <b>类名称：</b>CooperorgFormController <br/>
 * <b>类描述：</b><br/>
 * <b>修改人：</b><br/>
 * <b>修改备注：</b><br/>
 * <b>版本信息：</b>v1.0.0<br/>
 */
@Controller
@Scope("prototype")
@RequestMapping("/cooperorgForm")
public class CooperorgFormController extends CimsBaseController {
	
	@Autowired
	@Qualifier("com.jy.modules.cims.subsidiary.service.SubsidiaryService")
	public SubsidiaryService subsidiaryService;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.deal.service.LoanDealService")
	public LoanDealService loanDealService;
	
	
	/**
     * 通过 Controller 请求 跳转 进入至相关 页面
     */
    @RequestMapping(value = "/prepareExecute/{operate}")
    public ModelAndView execute(@PathVariable("operate") String operate) throws AbaboonException {
        ModelAndView model = new ModelAndView();
        
        if("toQuery".equals(operate)){//跳转至 查询页面
        	model.setViewName("cims/subsidiary/queryTCMmCooperorgForm");
        }
        
        return model;
    }

    /**
     *  统计查询合作机构的在途、转让、赎回、二次购买的债权价值
     * @Title getCooperorgFormTotalByDay
     * @Description TODO
     * @param request
     * @param dto
     * @param dataMsg
     * @return 
     * DataMsg 
     * @update 
     * @throws
     */
    @RequestMapping(value = "/getCooperorgFormTotalByDay")
	@ResponseBody
    public DataMsg getCooperorgFormTotalByDay(HttpServletRequest request, CooperorgFormCreditTotalDto dto, @ModelAttribute DataMsg dataMsg){
    	Map<String, Object> searchParams = new HashMap<String, Object>();
		searchParams.put("dto", dto);
		QueryReqBean params = new QueryReqBean();
		params.setSearchParams(searchParams);
		PageParameter pageInfo = PageUtil.toPageParameter(dataMsg);
		params.setPageParameter(pageInfo);
		
    	List<CooperorgFormCreditTotalDto> list = subsidiaryService.getCooperorgFormTotalByDay(params.getSearchParams());
		dataMsg.setData(list);
		dataMsg.setTotalRows(pageInfo.getTotalCount());
    	return dataMsg;
    }
    
    /**
     * 导出统计查询合作机构的在途、转让、赎回、二次购买的债权价值
     * @param request
     * @param dto
     * @param dataMsg
     * @return
     */
    @RequestMapping(value = "/exportCooperorgFormTotalByDay")
	@ResponseBody
    public void exportCooperorgFormTotalByDay(HttpServletRequest request,HttpServletResponse response, CooperorgFormCreditTotalDto dto, @ModelAttribute DataMsg dataMsg){
    	
		String dealDate = this.getParameterString("dealDate");
		String fileName = "合作机构报表"+dealDate;
    	
    	Map<String, Object> searchParams = new HashMap<String, Object>();
		searchParams.put("dto", dto);
		QueryReqBean params = new QueryReqBean();
		params.setSearchParams(searchParams);
		PageParameter pageInfo = PageUtil.toPageParameter(dataMsg);
		params.setPageParameter(pageInfo);
		
    	List<CooperorgFormCreditTotalDto> list = subsidiaryService.getCooperorgFormTotalByDay(params.getSearchParams());
    	ExportAndImport<CooperorgFormCreditTotalDto> exportAndImport = new ExportAndImport<CooperorgFormCreditTotalDto>();
    	String columnCode = CimsConstant.COOPERORG_FORM_CODE;
    	String columnName = CimsConstant.COOPERORG_FORM_NAME;
		HSSFWorkbook workbook;
		try {
			workbook = exportAndImport.exportExcelSubsidiary(fileName, columnCode, columnName, list);
			write(workbook, fileName, response);
		} catch (IOException e) {
			e.printStackTrace();
			dataMsg.failed(e.getMessage());
			throw new SysAssistException(CimsExceptionConstant.COOPERORG_FORM_FAILED + e.getMessage());
		}
    }
    
    /**
	 * 页面输出
	 * 
	 * @param workbook
	 * @param fileName
	 * @param response
	 * @throws IOException
	 */
	private void write(HSSFWorkbook workbook, String fileName, HttpServletResponse response) throws IOException {

		try {
			
			response.setContentType("application/vnd.ms-excel");
			response.setContentType("application/vnd.openxmlformatSs-officedocument.spreadsheetml.sheet");
			response.setCharacterEncoding("UTF-8");
			response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".xls");
			workbook.write(response.getOutputStream());
			response.getOutputStream().flush();
		} catch (Exception e) {
			throw new SysAssistException(CimsExceptionConstant.EXPORT_EXCEL_FAIL + e.getMessage());
		} finally {
			response.getOutputStream().close();
		}
	}

}